﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web.Mvc;
using Sharp.Platform.Mvc.Attribute;

namespace Sharp.Platform.Mvc.Bootstrap
{
    public static class DropdownListExtensions
    {
        /// <summary>
        /// 下拉框
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="title"></param>
        /// <returns></returns>
        public static DropdownListBuilder DropdownList(this BootstrapFactory factory, string title)
        {
            var ddl = factory.DropdownList();

            ddl.Title = title;

            return ddl;
        }
        /// <summary>
        /// 下拉框
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public static DropdownListBuilder DropdownList(this BootstrapFactory factory)
        {
            return new DropdownListBuilder();
        }
        /// <summary>
        /// 下拉框
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="factory"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static DropdownListBuilder DropdownListFor<TModel, TProperty>(this BootstrapFactory<TModel> factory, Expression<Func<TModel, TProperty>> expression)
        {
            return factory.UnobtrusiveValidationAttributes(factory.DropdownList(), expression);
        }
        /// <summary>
        /// 数据源
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static DropdownListBuilder DataSource(this  DropdownListBuilder factory, string id)
        {
            factory.Id(id);
            return factory;
        }
        /// <summary>
        /// 数据源
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="dataSource"></param>
        /// <returns></returns>
        public static DropdownListBuilder DataSource(this  DropdownListBuilder factory, IEnumerable dataSource)
        {
            factory.DataSource = dataSource;
            return factory;
        }
        /// <summary>
        /// 数据源
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="dataSource"></param>
        /// <returns></returns>
        public static DropdownListBuilder DataSource(this DropdownListBuilder factory, Type dataSource)
        {
            List<Enum> values = Enum.GetValues(dataSource).Cast<Enum>().ToList();

            factory.DataSource = values;
            return factory;
        }

        public static DropdownListBuilder DisplayField(this  DropdownListBuilder factory, string displayField)
        {
            factory.DisplayField = displayField;
            return factory;
        }
        public static DropdownListBuilder ValueField(this  DropdownListBuilder factory, string valueField)
        {
            factory.ValueField = valueField;
            return factory;
        }
        /// <summary>
        /// 选中值
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static DropdownListBuilder SelectedValue(this  DropdownListBuilder factory, Guid? selectedValue)
        {
            factory.SelectedValue = Convert.ToString(selectedValue);
            return factory;
        }
        /// <summary>
        /// 选中值
        /// </summary>
        /// <param name="factory"></param>
        /// <param name="selectedValue"></param>
        /// <returns></returns>
        public static DropdownListBuilder SelectedValue(this  DropdownListBuilder factory, string selectedValue)
        {
            factory.SelectedValue = Convert.ToString(selectedValue);
            return factory;
        }
        /// <summary>
        /// 简单型
        /// </summary>
        /// <param name="factory"></param>
        /// <returns></returns>
        public static DropdownListBuilder Simple(this  DropdownListBuilder factory)
        {
            factory.Simple = true;
            factory.Title = "请选择";
            return factory;
        }

        private static DropdownListBuilder UnobtrusiveValidationAttributes<TModel, TProperty>(this BootstrapFactory<TModel> factory, DropdownListBuilder builder, Expression<Func<TModel, TProperty>> expression)
        {
            var htmlHelper = factory.HtmlHelper;

            var name = ExpressionHelper.GetExpressionText(expression);

            var fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            var modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
            var attributes = htmlHelper.GetUnobtrusiveValidationAttributes(name, modelMetadata);

            builder.Id(fullHtmlFieldName).Name(fullHtmlFieldName);

            var value = htmlHelper.FormatValue(modelMetadata.Model, null);

            builder.SelectedValue(value);

            builder.Title = modelMetadata.DisplayName;


            var property = typeof(TModel).GetProperties().FirstOrDefault(x => x.Name == name);
            if (property != null)
            {
                var display =
                    property.GetCustomAttributes(typeof(DisplayValueFieldAttribute), false).FirstOrDefault() as
                        DisplayValueFieldAttribute;
                //if (display == null)
                //{
                //    throw new NotImplementedException("必须设置DisplayValueFieldAttribute特性");
                //}
                if (display != null)
                {
                    builder.DisplayField = display.DisplayField;
                    builder.ValueField = display.ValueField;
                }
            }
            foreach (var attribute in attributes)
                builder.Attribute(attribute.Key, attribute.Value.ToString(), true);

            return builder;
        }


    }
}
